import pygame, math, sys, random

class EnemyBase(pygame.sprite.Sprite):
    # Attributes or Variables
    # surface
    # rect
    # distToCenter
    # speed
    # maxSpeed
    # screenWidth
    # screenHeight

   
    
    # Methods or Functions
    def __init__(self, speed, position, screenSize):
        pygame.sprite.Sprite.__init__(self, self.containers)
        self.images = []
        imgs = ["rsc/imgs/enemies/enemybase.png"]
        
        for img in imgs:
            surf = pygame.image.load(img)
            x = surf.get_width()
            y = surf.get_height()
            surf = pygame.transform.scale(surf, (int(x*2.5),int(y*2.5)))
            self.images += [surf]
        self.frame = 0
        self.maxFrame = len(self.images)-1
        self.image = self.images[self.frame]
        self.rect = self.image.get_rect()
        self.dTC = self.rect.width/2
        self.speed = [0, 0]
        self.maxSpeed = 5
        self.detectionRadius = 200
        self.innerRadius = 100
        self.realx = position[0]
        self.realy = position[1]
        self.xOffset = 0
        self.place(position)
        self.screenWidth = screenSize[0]
        self.screenHeight = screenSize[1]
        self.place(position)
        self.health = 100
        self.living = True
        self.canShoot = False
        self.onGround = False
        self.canJump = 0
        self.jumpCount = 0
        self.startFall = True
        self.whatt = False
    
    def __str__(self):
        return "I am the base enemy" + str(self.rect.center), str(self.living)
        
    def place(self, position):
        self.rect.center = position
        #print "I've moved to", position
        
    def move(self):
        self.realx += self.speed[0]
        #self.realy += self.speed[1]
        self.place([self.realx - self.xOffset, self.realy])
        #print "I am moving", str(speed)
        
    def distToPoint(self, position):
        x1 = self.rect.center[0]
        x2 = position[0]
        y1 = self.rect.center[1]
        y2 = position[1]
        
        #return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
        
    def chase(self, player):
        if self.distToPoint(player.rect.center) < self.detectionRadiusm and self.distToPoint(player.rect.center) > self.innerRadius:
                pX = player.rect.center[0]
                zX = self.rect.center[0]
                
                if pX > zX:
                    self.speed[0] = self.maxSpeed
                elif pX < zX:
                    self.speed[0] = -self.maxSpeed
                else:
                    self.speed[0] = self.noSpeed
                return True
        return False
        
    def stop(self, player):
        if self.distToPoint(player.rect.center) < self.detectionRadius and self.distToPoint(player.rect.center) < self.innerRadius:
                self.speed = [0, 0]
                self.canShoot = True
                return True
        else:
            if self.speed == [0,0]:
                self.randomDirection()
                return False
    
    def randomDirection(self):
        while self.speed == [0,0]:
            xmult = random.randint(-1,1)
            self.speed = [self.maxSpeed * xmult] 
    
    def shoot(self, player):
        pass
        
    def wallCollide(self):
        if (self.rect.left < 0 
            or self.rect.right > self.screenWidth):
            self.speed[0] = self.speed[0]*-1
        if (self.rect.top < 0 
            or self.rect.bottom >self.screenHeight):
            self.speed[1] = self.speed[1]*-1
            
    def fall(self):
        if self.onGround == False:
            if not self.canJump == 1:
                if self.startFall == True:
                    self.speed[1] = 0
                    self.startFall = False
                if self.jumpCount < 5:
                    self.jumpCount +=1
                elif self.jumpCount == 5:
                    self.speed[1] += 2
                    self.jumpCount = 0
        
    def update(*args):
        self = args[0]
        self.move()
        self.wallCollide()
        #self.chase(player)
        #self.stop(player)
        self.randomDirection()
        self.fall()
        #self.shoot(player)
    